package com.lee.dhsg.controller;

import com.lee.dhsg.domain.Category;
import com.lee.dhsg.domain.Lunbo;
import com.lee.dhsg.domain.Teacher;
import com.lee.dhsg.service.CategoryService;
import com.lee.dhsg.service.CourseService;
import com.lee.dhsg.domain.Course;
import com.lee.dhsg.service.LunboService;
import com.lee.dhsg.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：该控制器用于提供初始化数据给首页
 * 1、轮播所需要的三个课程图片
 * 2、类别数据，包括一级类别和二级类别
 * 3、推荐的课程
 * 4、推荐的教师
 *
 * @author Lee
 */

@Controller
public class IndexController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private LunboService lunboService;

    @RequestMapping("/index")
    protected String index(Model m) throws Exception {

        //1、轮播所需要的图片
        List<Lunbo> lunboList = lunboService.findAll();
        m.addAttribute("lunboList", lunboList);

        //2、一级类别
        List<Category> firstCategories = categoryService.selectAllFirstCategory();
        m.addAttribute("firstCategories", firstCategories);

        //3、二级类别
        List<List<Category>> secondCategories = new ArrayList<>();
        for (Category firstCategorie : firstCategories) {
            secondCategories.add(categoryService.selectAllSecondCategoryByFirstCategoryId(firstCategorie.getCategoryid()));
        }
        m.addAttribute("secondCategories", secondCategories);

        //4、推荐的课程
        List<Course> courses = courseService.selectByTuijian();
        m.addAttribute("courses", courses);

        //5、推荐的教师
        List<Teacher> teachers = teacherService.selectByTuijian();
        m.addAttribute("teachers", teachers);

        //跳转至 index.jsp 页面
        return "index";

    }

}
